package com.kang.service.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.kang.dao.SupplyAgreementMapper;
import com.kang.pojo.SupplyAgreement;
import com.kang.service.SupplyAgreementService;
import com.vonechina.entity.page.SupplyAgreementPage;

/**
 * 供货协议服务层实现类
 * 
 * @author zs614
 *
 */
@Service
public class SupplyAgreementServiceImpl implements SupplyAgreementService {
	/**
	 * 供货协议持久层
	 */
	@Resource
	private SupplyAgreementMapper sa;

	/**
	 * 分页查询供货协议
	 * 
	 * @param supplyAgreementPage
	 *            搜索条件实体类
	 * @return
	 */
	@Override
	public List<SupplyAgreement> listAgreement(SupplyAgreementPage supplyAgreementPage) {
		supplyAgreementPage.setRows(sa.countAgreement(supplyAgreementPage));
		List<SupplyAgreement> list = sa.listAgreement(supplyAgreementPage);
		return list;
	}

	/**
	 * 删除供货协议
	 * 
	 * @param supplyagreementid
	 *            供货协议ID
	 * @return
	 */
	@Override
	public int deleteByPrimaryKey(Integer supplyagreementid) {
		return sa.deleteByPrimaryKey(supplyagreementid);
	}

	/**
	 * 新增供货协议
	 * 
	 * @param record
	 *            供货协议实体类
	 * @return
	 */
	@Override
	public int insert(SupplyAgreement record) {
		return sa.insert(record);
	}

	/**
	 * 新增供货协议
	 * 
	 * @param record
	 *            供货协议实体类
	 * @return
	 */
	@Override
	public int insertSelective(SupplyAgreement record) {
		return sa.insertSelective(record);
	}

	/**
	 * 通过ID查询供货协议
	 * 
	 * @param supplyagreementid
	 *            供货协议ID
	 * @return
	 */
	@Override
	public SupplyAgreement selectByPrimaryKey(Integer supplyagreementid) {
		return sa.selectByPrimaryKey(supplyagreementid);
	}

	/**
	 * 更新供货协议
	 * 
	 * @param record
	 *            供货协议实体类
	 * @return
	 */
	@Override
	public int updateByPrimaryKeySelective(SupplyAgreement record) {
		return sa.updateByPrimaryKeySelective(record);
	}

	/**
	 * 更新供货协议
	 * 
	 * @param record
	 *            供货协议实体类
	 * @return
	 */
	@Override
	public int updateByPrimaryKey(SupplyAgreement record) {
		return sa.updateByPrimaryKey(record);
	}

	/**
	 * 生成供货协议编号
	 * 
	 * @return
	 */
	@Override
	public String generatoCode() {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
		String text = sdf.format(new Date()) + "0001";
		Long no = Long.parseLong(text);
		String max = sa.selectMaxCode();
		Long l = 0l;
		if (max != null) {
			l = Long.parseLong(max.replaceAll("[a-zA-Z]", ""));
		}
		if (l >= no) {
			no = l + 1;
		}
		return "GHXY" + no;
	}

}
